package cn.iocoder.yudao.module.ent.service.receivenumber;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.ent.controller.admin.receivenumber.vo.ReceiveNumberPageReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.receivenumber.vo.ReceiveNumberSaveReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.receivenumber.vo.ReceiveNumberUpdateReqVO;
import cn.iocoder.yudao.module.ent.convert.receivenumber.ReceiveNumberConverter;
import cn.iocoder.yudao.module.ent.dal.dataobject.receivenumber.ReceiveNumberDO;
import cn.iocoder.yudao.module.ent.dal.mysql.receivenumber.ReceiveNumberMapper;
import cn.iocoder.yudao.module.ent.document.node.NodeContactList;
import cn.iocoder.yudao.module.ent.enums.EntConstants;
import cn.iocoder.yudao.module.ent.enums.EntFollowType;
import cn.iocoder.yudao.module.ent.mq.message.receive.ReceiveSendMessage;
import cn.iocoder.yudao.module.ent.mq.producer.follow.FollowProducer;
import cn.iocoder.yudao.module.ent.service.search.SearchService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ent.enums.ErrorCodeConstants.RECEIVE_NUMBER_NOT_EXISTS;
import static cn.iocoder.yudao.module.ent.enums.ErrorCodeConstants.RECEIVE_NUMBER_UNKNOWN_TYPE;

/**
 * 联系号码 Service 实现类
 *
 * @author 我
 */
@Service
@Validated
public class ReceiveNumberServiceImpl implements ReceiveNumberService {

    @Resource
    private ReceiveNumberMapper receiveNumberMapper;

    @Resource
    private SearchService searchService;

    @Resource
    private FollowProducer followProducer;


    @Override
    public Long createReceiveNumber(ReceiveNumberSaveReqVO createReqVO) {
        // 插入
        ReceiveNumberDO receiveNumber = BeanUtils.toBean(createReqVO, ReceiveNumberDO.class);
        receiveNumberMapper.insert(receiveNumber);
        // 返回
        return receiveNumber.getId();
    }

    @Override
    public void updateReceiveNumber(ReceiveNumberUpdateReqVO updateReqVO) {

        ReceiveNumberDO receiveNumberDO = receiveNumberMapper.selectById(updateReqVO.getId());
        if (receiveNumberDO == null) {
            throw exception(RECEIVE_NUMBER_NOT_EXISTS);
        }
        ReceiveNumberDO updateObj = new ReceiveNumberDO();
        updateObj.setFollowStatus(Boolean.TRUE);
        updateObj.setId(updateReqVO.getId());
        if (EntFollowType.isTag(updateReqVO.getType())) {
            updateObj.setTag(updateReqVO.getTag());
            followProducer.sendReceiveMessage(EntFollowType.TAG.getType(), SecurityFrameworkUtils.getLoginUserId(), receiveNumberDO.getEntId(), receiveNumberDO.getNumber(), updateReqVO.getTag(), null, null, null);
        } else if (EntFollowType.isRemark(updateReqVO.getType())) {
            updateObj.setRemark(updateReqVO.getRemark());
            followProducer.sendReceiveMessage(EntFollowType.REMARK.getType(), SecurityFrameworkUtils.getLoginUserId(), receiveNumberDO.getEntId(), receiveNumberDO.getNumber(), updateReqVO.getRemark(), null, null, null);
        }
        receiveNumberMapper.updateById(updateObj);
    }

    @Override
    public void simpleUpdateReceiveNumber(ReceiveNumberDO data) {
        receiveNumberMapper.updateById(data);
    }

    @Override
    public void deleteReceiveNumber(Long id) {
        // 校验存在
        validateReceiveNumberExists(id);
        // 删除
        receiveNumberMapper.deleteById(id);
    }

    @Override
    public void batchDeleteReceiveNumberByEntId(String entId) {
        receiveNumberMapper.delete(EntConstants.FOREIGN_KEY, entId);
    }

    private void validateReceiveNumberExists(Long id) {
        if (receiveNumberMapper.selectById(id) == null) {
            throw exception(RECEIVE_NUMBER_NOT_EXISTS);
        }
    }

    @Override
    public ReceiveNumberDO getReceiveNumber(Long id) {
        return receiveNumberMapper.selectById(id);
    }

    @Override
    public ReceiveNumberDO getReceiveNumber(String entId, String number) {
        return receiveNumberMapper.selectOne(new LambdaQueryWrapperX<ReceiveNumberDO>()
                .eq(ReceiveNumberDO::getEntId, entId)
                .eq(ReceiveNumberDO::getNumber, number)
                , false);
    }

    @Override
    public List<ReceiveNumberDO> getReceiveNumberList(String entId) {
        return receiveNumberMapper.selectList(EntConstants.FOREIGN_KEY, entId);
    }

    @Override
    public PageResult<ReceiveNumberDO> getReceiveNumberPage(ReceiveNumberPageReqVO pageReqVO) {
        return receiveNumberMapper.selectPage(pageReqVO);
    }

    @Override
    public void doCreateReceiveNumber(ReceiveSendMessage message) {
        List<NodeContactList> contactLists = searchService.queryContactListByIdList(message.getIdList());
        List<ReceiveNumberDO> convert = ReceiveNumberConverter.INSTANCE.convert(contactLists, message.getUserId());
        receiveNumberMapper.insertBatch(convert);
    }
}